package com.loukou.lkt.lkt.utils;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.log4j.Logger;

import com.fasterxml.jackson.databind.ObjectMapper;

public abstract class PostUtils {
	static Logger logger = Logger.getLogger(PostUtils.class);
	
	public static Object postData(String url, Object object){
	   try{
		   HttpPost httpPost = new HttpPost(url);
		   ObjectMapper mapper = new ObjectMapper();
		   StringEntity entity = new StringEntity(mapper.writeValueAsString(object),"UTF-8");
		   entity.setContentType("application/json;charset=UTF-8");
		   entity.setContentEncoding("UTF-8");
		   httpPost.setEntity(entity);
		   HttpClient httpClient = HttpClients.createDefault();
		   HttpResponse response  = httpClient.execute(httpPost);
		   httpPost.releaseConnection();
		   HttpEntity resEntity = response.getEntity();
		   Object result =  mapper.readValue(resEntity.getContent(), Object.class);
		   return result;
	   }catch(Exception ex){
		   logger.error("向"+url+"POST数据错误:" + ex.getMessage());
	   }
	   return null;
	}  

}
